package com.cmas.platform.controller;

import java.util.Arrays;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import com.aidex.common.annotation.DataSource;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.TableDataInfo;
import com.aidex.common.core.page.PageDomain;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aidex.common.enums.BusinessType;
import com.aidex.common.enums.DataSourceType;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.common.core.controller.BaseController;
import com.aidex.framework.cache.DictUtils;

import javax.validation.constraints.*;

import com.aidex.system.service.ISysUserService;
import com.cmas.platform.vo.NotificationVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.cmas.platform.domain.Notification;
import com.cmas.platform.service.NotificationService;


/**
 * 推送表Controller
 *
 * @author su
 * @email 1218819874@qq.com
 * @date 2024-05-14
 */
@DataSource(value = DataSourceType.SLAVE)
@RestController
@RequestMapping("/platform/notification")
public class NotificationController extends BaseController {

    @Autowired
    private NotificationService notificationService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询推送表列表
     */
    @PreAuthorize("@ss.hasPermi('platform:notification:list')")
    @GetMapping("/list")
    public R<TableDataInfo> list(Notification notification) {
        startPage();
        List<NotificationVo> list = notificationService.selectNotificationVoList(notification);
        return R.data(getDataTable(list));
    }

    /**
     * 获取推送表详细信息
     */
    @PreAuthorize("@ss.hasPermi('platform:notification:query')")
    @GetMapping(value = "/{id}")
    public R<Notification> detail(@PathVariable("id") String id) {
        return R.data(notificationService.getById(id));
    }

    /**
     * 新增推送表
     */
    @PreAuthorize("@ss.hasPermi('platform:notification:add')")
    @Log(title = "推送表", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated Notification notification) {
        return R.data(notificationService.addNotification(notification));
    }

    /**
     * 修改推送表
     */
    @PreAuthorize("@ss.hasPermi('platform:notification:edit')")
    @Log(title = "推送表", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated Notification notification) {
        return R.data(notificationService.updateById(notification));
    }

    /**
     * 删除推送表
     */
    @PreAuthorize("@ss.hasPermi('platform:notification:remove')")
    @Log(title = "推送表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.data(notificationService.removeByIds(Arrays.asList(ids)));
    }


    /**
     * 导出推送表列表
     */
    @PreAuthorize("@ss.hasPermi('platform:notification:export')")
    @Log(title = "推送表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(Notification notification) {
        List<Notification> list = notificationService.selectNotificationList(notification);
        ExcelUtil<Notification> util = new ExcelUtil<Notification>(Notification.class);
        return util.exportExcel(list, "推送表数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }


    // ***用户系统位置戳****


    /**
     * 获得用户最后一条通知列表
     *
     * @param notification
     * @return
     */
    @GetMapping("/listLastNotification")
    public R<?> listLastNotification(Notification notification,
                                     @RequestParam(name = "pageNo", defaultValue = "0") Integer pageNo,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return R.data(notificationService.selectLastNotificationVoList(pageNo, pageSize, notification));
    }


    /**
     * 获得通知列表
     *
     * @param notification
     * @return
     */
    @GetMapping("/notificationDetailList")
    public R<?> notificationDetailList(Notification notification) {
        startPage();
        return R.data(getDataTable(notificationService.selectNotificationDetailList(notification)));
    }


    /**
     *  获取未读通知条数
     *
     * @param
     * @return
     */
    @GetMapping("/unReadNotificationCount")
    public R<?> unReadNotificationCount(Notification notification) {
        return R.data(notificationService.unReadNotificationCount(notification));
    }

}
